var t = require("../../@babel/runtime/helpers/defineProperty"), a = require("../../@babel/runtime/helpers/toConsumableArray"), o = getApp();

Page({
    data: {
        cartsNum: 0,
        cartList: [],
        categoryList: [],
        activeCat: null,
        goodList: [],
        sortOption: [ {
            value: "id",
            name: "综合排序"
        }, {
            value: "price",
            name: "价格最低"
        }, {
            value: "discount",
            name: "优惠力度"
        } ],
        activeSort: "id",
        activity_gid: 0,
        activity_index: 0,
        showgp: !1,
        popupSpecs: !1,
        goodinfo: {},
        freight: null,
        page: 1,
        isLoadMore: !0,
        total_page: 0,
        orderCart: [],
        showCarList: !1,
        dispatch_types: [],
        showChoose: !1,
        radioChoose: ""
    },
    options: {
        multipleSlots: !0
    },
    onLoad: function(t) {
        if (t.cart_id) {
            var a = JSON.parse(t.cart_id);
            "" === a[0] && (a = []), this.data.orderCart = a.map(Number);
        }
        this.getCategory();
    },
    getCategory: function() {
        var t = this, a = o.getNetAddresss("goods.postage-included-category.index");
        o._postNetWork({
            url: a,
            data: {},
            success: function(a) {
                var o = a.data;
                1 == o.result ? (t.setData({
                    categoryList: o.data
                }), o.data && o.data.length > 0 && (t.setData({
                    activeCat: o.data[0].id
                }), t.changeSort())) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    changeCategory: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            activeCat: a
        }), this.changeSort();
    },
    changeSort: function(t) {
        var a = this, s = null;
        t && t.currentTarget.dataset && t.currentTarget.dataset.value && (s = t.currentTarget.dataset.value, 
        this.setData({
            activeSort: s
        })), this.setData({
            goodList: []
        });
        var e = {
            category_id: this.data.activeCat,
            sort: s || "id"
        }, i = o.getNetAddresss("goods.postage-included-category.goods");
        o._postNetWork({
            url: i,
            data: e,
            success: function(t) {
                var o = t.data;
                1 == o.result ? (a.setData({
                    goodList: o.data.data,
                    isLoadMore: !0,
                    total_page: o.data.last_page,
                    page: 1
                }), a.getCartList()) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getCartList: function() {
        var t = this, a = o.getNetAddresss("postageIncludedCategory.member-cart.index");
        o._postNetWork({
            url: a,
            data: {},
            success: function(a) {
                var o = a.data;
                1 == o.result ? (t.setData({
                    cartList: o.data
                }), t.setGoodTotal(), t.getFreight()) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    setGoodTotal: function() {
        this.data.cartsNum = 0;
        for (var t = 0; t < this.data.cartList.length; t++) {
            this.data.cartsNum += this.data.cartList[t].total;
            for (var a = 0; a < this.data.goodList.length; a++) if (0 == t && this.data.goodList[a].total && (this.data.goodList[a].total = 0), 
            this.data.cartList[t].goods_id == this.data.goodList[a].id) {
                var o = 0;
                0 == this.data.cartList[t].option_id ? o = this.data.cartList[t].total : (o = this.data.goodList[a].total || 0, 
                o += this.data.cartList[t].total), this.data.goodList[a].total = o, this.data.goodList[a].card_id = this.data.cartList[t].id;
            }
        }
        this.setData({
            cartsNum: this.data.cartsNum,
            goodList: this.data.goodList
        });
    },
    getFreight: function() {
        var t = this, s = [];
        this.data.cartList.forEach(function(t) {
            s.push(t.id);
        });
        var e = s.concat(this.data.orderCart), i = new Set(e);
        i = a(i);
        var d = o.getNetAddresss("postageIncludedCategory.cart-list.index");
        o._postNetWork({
            url: d,
            data: {
                cart_ids: i.join(",")
            },
            success: function(a) {
                var o = a.data;
                1 == o.result ? t.setData({
                    freight: o.data.postage_included_msg && "" != o.data.postage_included_msg ? Number(o.data.postage_included_msg) : 0
                }) : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    _getMoreData: function() {
        var t = this, a = o.getNetAddresss("goods.postage-included-category.goods");
        t.data.isLoadMore = !1, this.data.page >= this.data.total_page || (this.data.page = this.data.page + 1, 
        o._postNetWork({
            url: a,
            showToastIn: !1,
            data: {
                page: this.data.page,
                category_id: this.data.activeCat,
                sort: this.data.activeSort
            },
            success: function(a) {
                var o = a.data;
                1 == o.result ? t.setData({
                    isLoadMore: !0,
                    goodList: t.data.goodList.concat(o.data.data)
                }) : t.setData({
                    page: t.data.page - 1,
                    isLoadMore: !1
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        }));
    },
    addCart: function(t) {
        console.log(t.detail);
        var a = t.detail;
        this.setData({
            activity_index: a.index
        }), a.isSpecs ? this.showGoodDetail(a, !0) : this.addMemberCart({
            id: a.id,
            total: "1",
            index: a.index
        });
    },
    addMemberCart: function(a) {
        var s = this;
        if (a.id) {
            var e = {
                goods_id: a.id,
                total: a.total || 1,
                option_id: a.option_id || 0
            }, i = o.getNetAddresss("postageIncludedCategory.member-cart.store");
            o._postNetWork({
                url: i,
                data: e,
                success: function(o) {
                    var e = o.data;
                    wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1e3
                    }), 1 == e.result && s.setData(t({
                        popupSpecs: !1,
                        showgp: !1
                    }, "goodList[" + a.index + "].total", a.total)), s.getCartList();
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    showGoodDetail: function(a, s) {
        var e = this, i = a.detail ? a.detail : a;
        if (this.data.activity_gid != i.id) {
            this.setData({
                activity_gid: i.id
            });
            var d = o.getNetAddresss("goods.goods.get-goods-page");
            o._postNetWork({
                url: d,
                data: {
                    id: Number(i.id)
                },
                success: function(a) {
                    var o = a.data;
                    if (1 == o.result) {
                        var i, d = s ? "popupSpecs" : "showgp";
                        e.setData((t(i = {}, d, !0), t(i, "goodinfo", o.data.get_goods), i));
                    } else wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1e3
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else {
            var r = s ? "popupSpecs" : "showgp";
            this.setData(t({}, r, !0));
        }
    },
    close_yz_specs_popup: function(t) {
        this.setData({
            popupSpecs: !1
        }), 1 == t.detail.clicktype && (t.detail.cartid ? this.updateCart(t.detail.cartid, t.detail.goodsCount) : this.addMemberCart({
            id: this.data.activity_gid,
            total: t.detail.goodsCount,
            option_id: t.detail.optionsId || 0,
            index: this.data.activity_index
        }));
    },
    openCartListPopup: function() {
        this.setData({
            showCarList: !0
        });
    },
    close_yz_cartList_popup: function() {
        this.setData({
            showCarList: !1
        });
    },
    changeCart: function(a) {
        var s = this, e = a.detail ? a.detail : a;
        if (e.cartId) if (0 != Number(e.num)) {
            var i = o.getNetAddresss("cart.list.updateNumV2");
            o._postNetWork({
                url: i,
                data: {
                    id: e.cartId,
                    num: e.num
                },
                success: function(a) {
                    var o = a.data;
                    wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1e3
                    }), 1 == o.result && (s.setData({
                        popupSpecs: !1,
                        showgp: !1
                    }), e.index && s.setData(t({}, "goodList[" + e.index + "].total", e.num)), s.getCartList());
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        } else this.destroyCart(e.cartId);
    },
    destroyCart: function(t) {
        var a = this, s = o.getNetAddresss("cart.list.destroy");
        o._postNetWork({
            url: s,
            data: {
                ids: t
            },
            success: function(t) {
                var o = t.data;
                wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                }), a.getCartList();
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    changeCarList: function(t) {
        var a = "plus" == t.detail.name ? t.detail.total + 1 : "minus" == t.detail.name ? t.detail.total - 1 : t.detail.total;
        this.changeCart({
            cartId: t.detail.id,
            num: a
        });
    },
    typeChange: function(t) {
        this.setData({
            radioChoose: Number(t.detail)
        });
    },
    checkGoods: function() {
        var t = this, s = [];
        this.data.cartList.forEach(function(t) {
            s.push(t.id);
        });
        var e = s.concat(this.orderCart), i = new Set(e);
        i = a(i);
        var d = o.getNetAddresss("memberCart.checkout");
        d += "&cart_ids=" + i.join(","), o._getNetWork({
            url: d,
            success: function(a) {
                var o = a.data;
                1 == o.result ? 1 == o.data.need_choose ? t.setData({
                    dispatch_types: o.data.dispatch_types,
                    showChoose: !0
                }) : t.toOrder() : wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    toOrder: function() {
        this.initData();
        var t = [], o = [];
        if (!this.data.showChoose || this.data.radioChoose) {
            if (this.data.showChoose) for (var s = 0; s < this.data.dispatch_types[this.data.radioChoose].member_carts.length; s++) o.push(this.data.dispatch_types[this.data.radioChoose].member_carts[s].id); else {
                this.data.cartList.forEach(function(a) {
                    t.push(a.id);
                });
                var e = t.concat(this.data.orderCart);
                o = new Set(e), o = a(o);
            }
            setTimeout(function() {
                wx.navigateTo({
                    url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-1&cart_ids=" + JSON.stringify(o)
                });
            }, 80);
        } else wx.showToast({
            icon: "none",
            title: "请先选择配送方式",
            duration: 1e3
        });
    },
    initData: function() {
        this.setData({
            showgp: !1,
            popupSpecs: !1,
            showCarList: !1,
            showChoose: !1
        });
    },
    showgoodspecs: function() {
        this.setData({
            popupSpecs: !0
        });
    },
    closeChoose: function() {
        this.setData({
            showChoose: !1
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this._getMoreData() : console.log("没有更多数据");
    },
    onShareAppMessage: function() {}
});